#!/usr/bin/env bash

help(){
    cat<<__EOF__
Usage: m bluetooth [OPTIONS]

Description: Manage Bluetooth settings on macOS

Options:
  --help       Show this help message
  --status     Show the current Bluetooth status
  --enable     Turn on bluetooth
  --disable    Turn off bluetoothd
__EOF__
}

[ -e /usr/sbin/bluetoothd ] && DAEMON=bluetoothd || DAEMON=blued

case $1 in
    --help)
        help
        ;;
    --status)
        defaults read /Library/Preferences/com.apple.Bluetooth ControllerPowerState | \
            awk '{ if($1 != 0) {print "Bluetooth: Enabled"} else { print "Bluetooth: Disabled" }  }'
        ;;
    --enable)
        sudo defaults write \
            /Library/Preferences/com.apple.Bluetooth ControllerPowerState -int 1 && \
            sudo killall -HUP ${DAEMON}
        ;;
    --disable)
        sudo defaults write \
            /Library/Preferences/com.apple.Bluetooth ControllerPowerState -int 0 && \
            sudo killall -HUP ${DAEMON}
        ;;
    *)
        help && exit 1
        ;;
esac
